package com.md.api.common.interceptor;


import com.md.api.common.exception.BusinessException;
import com.md.api.entity.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@ControllerAdvice
public class DefaultExceptionHandler {
	

    @ResponseBody
	@ExceptionHandler(value = BusinessException.class)
	public HttpResult businessException(HttpServletRequest request, BusinessException e){
    	log.info("BusinessException:{}", e.getMsg());
    	return new HttpResult(e.getCode() == null ? 503 : e.getCode(), e.getMsg());
	}
    

    @ResponseBody
	@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)  
	public HttpResult methodNotSupportException(HttpServletRequest request, HttpRequestMethodNotSupportedException e){
    	log.info("HttpRequestMethodNotSupportedException:{}", e.getMessage());
    	return HttpResult.METHOD_NOT_SUPPORTED;
	}
    
    @ResponseBody 
	@ExceptionHandler(value = Exception.class)  
	public HttpResult exception(HttpServletRequest request, Exception e){
    	log.info("exception", e);
    	return HttpResult.SYSTEM_ERROR;
	}
   
}
